<template>
  <div>
    <!-- /还差一个详情页面 -->
      <div style="margin-bottom: 10px;margin-top: 10px;">
          <el-input v-model="userName"  placeholder="请输入法人姓名" suffix-icon="el-icon-search" style="width: 200px"
          @keyup.enter.native="loadPost"></el-input>
          <el-select v-model="unitId" placeholder="请选择机构" style="margin-left: 10px">
            <el-option
             v-for="item in units"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
          </el-select>
          <!-- 会员 -->

          <el-select style="margin-left: 10px" v-model="role" filterable placeholder="请选择角色">
              <el-option
                      v-for="item in roles"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
              </el-option>
          </el-select>
     
          <el-button type="primary" style="margin-left: 10px" @click="loadPost">查询</el-button>
          <el-button type="success" @click="resetParam">重置</el-button>
          <el-button type="primary" style="margin-left: 10px" @click="add">新增</el-button>
      </div>
  <el-table :data="tableData"
  :header-cell-style="{background:'#f2f5fc',color:'#555'}" border>
      <el-table-column prop="id" label="ID" width="160">
      </el-table-column>

      <el-table-column prop="userName" label="账号" width="160">
      </el-table-column>
 

    <el-table-column label="身份" width="120">
        <template slot-scope="scope">
            <el-tag type="success" effect="dark" v-if="scope.row.role==1">普通用户</el-tag>
            <el-tag type="warning" effect="dark" v-if="scope.row.role==2">管理员</el-tag>
            <el-tag type="danger" effect="dark" v-if="scope.row.role==3">超级管理员</el-tag>
        </template>
    </el-table-column>


      <el-table-column prop="name" label="真实姓名"  width="160">
      </el-table-column>

      <el-table-column  label="性别"  width="160">
        <template slot-scope="scope">
            <el-tag type="primary" effect="dark" v-if="scope.row.sex==1">男</el-tag>
            <el-tag type="danger" effect="dark" v-if="scope.row.sex==2">女</el-tag>
        </template>
      </el-table-column>


      <el-table-column prop="phone" label="移动手机号"  width="160">
      </el-table-column>

      <el-table-column prop="unitId" label="所属机构"  width="160" :formatter="unitFormate">
      </el-table-column>
      
      <el-table-column prop="createTime" label="时间"  width="160">
      </el-table-column>
      
      <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
      
          <el-button size="small" type="success">详情</el-button>
              
          <el-popconfirm
                      style="margin-left: 5px;"
                      title="这是一段内容确定删除吗？"
                      @confirm="del(scope.row.id)"
              >
                  <el-button slot="reference" size="small" type="danger">删除</el-button>
              </el-popconfirm>
          </template>
      </el-table-column>

  </el-table>
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pagenum"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>

      <el-dialog
              title="提示"
              :visible.sync="centerDialogVisible"
              width="80%"
              center>
<!--           //增加表单-->
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          
              <el-form-item label="账号" prop="userName">
                      <el-input  v-model="form.userName"></el-input>
              </el-form-item>

              
              <el-form-item label="角色">
                  <el-select v-model="form.role" placeholder="请选择属于自己的角色">
                  <el-option label="普通用户" value="1"></el-option>
                  <el-option label="机构管理员" value="2"></el-option>
                  <el-option label="系统管理员" value="3"></el-option>
                   </el-select>
                </el-form-item>

              <el-form-item label="真实姓名" prop="name">
                      <el-input  v-model="form.name"></el-input>
              </el-form-item>

              <el-form-item label="身份证" prop="idCard">
                      <el-input  v-model="form.idCard"></el-input>
              </el-form-item>

              <el-form-item label="性别">
                  <el-select v-model="form.sex" placeholder="请选择属于自己的性别">
                  <el-option label="男" value="1"></el-option>
                  <el-option label="女" value="2"></el-option>
                   </el-select>
                </el-form-item>

                <el-form-item label="移动手机号" prop="phone">
                      <el-input  v-model="form.phone"></el-input>
              </el-form-item>

             
              
              <el-form-item label="学历">
                  <el-select v-model="form.educ" placeholder="请选择属于自己的学历">
                  <el-option label="专科" value="1"></el-option>
                  <el-option label="本科" value="2"></el-option>
                  <el-option label="其他" value="3"></el-option>
                   </el-select>
                </el-form-item>

                <el-form-item label="职称" prop="jobName">
                      <el-input  v-model="form.jobName"></el-input>
              </el-form-item>

              <el-form-item label="职务" prop="jobPosition">
                      <el-input  v-model="form.jobPosition"></el-input>
              </el-form-item>


             
             
                   <el-form-item label="所属机构">
                  <el-select v-model="form.unitId" placeholder="请选择属于自己的所属机构">
                    <el-option
                            v-for="item in units"
                            :key="item.id"
                            :label="item.name"
                     :value="item.id">
                    </el-option>
 
                   </el-select>
                </el-form-item>

                <el-form-item label="是否参加过培训">
                  <el-select v-model="form.isTraining" placeholder="是否参加过培训">
                  <el-option label="是" value="1"></el-option>
                  <el-option label="否" value="2"></el-option>
                 
                   </el-select>
                </el-form-item>

                <el-form-item label="培训时长" prop="trainingTime">
                      <el-input  v-model="form.trainingTime"></el-input>
              </el-form-item>

        
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="save">确 定</el-button>
            </span>
      </el-dialog>
  </div>
</template>

<script>
    export default {
        name: "UnitList",
        data() {
         
            return {
          
        unitId: -1,
        units:[],
        roles:[{
          value: -1,
          label: '所有角色'
        },
        {
          value: 1,
          label: '普通用户'
        },
        {
          value: 2,
          label: '管理员'
        },
        {
          value: 3,
          label: '超级管理员'
        }],
        role:-1,
        userName:'',
      
        units:[],
        tableData: [],
        pagenum:1,
        pageSize:2,
        total:0,
        name:'',
        session:'',
        
        centerDialogVisible:false,
        form:{
            id:'',
            jobPosition:'',
            unitId:'',
            isTraining:'',
            trainingTime:'',
            jobName:'',
            educ:'',
            phone:'',
            sex:'',
            idCard:'',
            name:'',
            role:'',
            userName:'',
            createUserId:'',
             createUserName:''
                },
                rules: {
                   
                    name: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                    ],
                   

                }

            }

        },
        created(){
            this.session = JSON.parse(sessionStorage.getItem('CurUser'))
            this.form.createUserId=this.session.createUserId
            this.form.createUserName=this.session.createUserName
           
        },
        methods:{
        // 显示机构
        
        unitFormate(row, index){
            
                for(let i in this.units){
                    if(this.units[i].id==row.unitId){
                      
                        return this.units[i].name
                    }
                }

            },
            // 在数组中头部增加一个数据
            plce(){
                let newList = {
                id:'-1',
                name:'无',
                }
                this.units.unshift(newList)
            },
             // 显示状态
             statusFormate(row, index){
                if(row.unitId == 0){
                    return "无";
                }
            },
            resetForm() {
                this.$refs.form.resetFields();
            },
            mod(row){
              
                this.centerDialogVisible=true
                this.$nextTick(()=>{
                    this.form.id = row.id
                    this.form.userName = row.userName
                    this.form.role = row.role
                    this.form.name = row.name
                    this.form.idCard = row.idCard
                    this.form.phone = row.phone
                    this.form.educ = row.educ
                    this.form.jobName = row.jobName
                    this.form.trainingTime = row.trainingTime
                    this.form.isTraining = row.isTraining
                    this.form.jobPosition = row.jobPosition
                    this.form.unitId = row.unitId
                    this.form.sex = row.sex
                    this.form.createUserId=this.session.createUserId
                    this.form.createUserName=this.session.createUserName
                 
                    //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
            },
            // 删除
            del(id){
                this.$axios.delete(this.$httpUrl+'/jk/admin/user/remove?id='+id).then(res=>res.data).then(res=>{

                    if (res.resCode=="000000"){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面
                        this.loadPost()
                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })

            },
            add(){

                this.centerDialogVisible=true
                this.$nextTick(()=>{
                    this.resetForm()
                //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
            },
            // 修改
            doMod(){
                this.$axios.put(this.$httpUrl+'/jk/admin/user/update',
                    this.form

                ).then(res=>res.data).then(res=>{

                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            // 增加保存
            doSave(){
              
                this.$axios.post(this.$httpUrl+'/jk/admin/user/add',
                    this.form

                ).then(res=>res.data).then(res=>{
                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            save(){
                this.$refs.form.validate((valid) => {
                    if (valid) {                
                        if (this.form.id){                          
                            this.doMod()
                        }else{                         
                            this.doSave()
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            handleSizeChange(val) {
                
                this.pagenum=1
                this.pageSize=val
                this.loadPost()
            },
            handleCurrentChange(val) {
                
                this.pagenum=val
                this.loadPost()
            },
          
            resetParam(){
                this.title='',
                this.unitId=-1,
                this.roles=-1
                
            },
            loadPost(){
            
             this.$axios.get(this.$httpUrl+'/jk/admin/user/list?unitId='+this.unitId+'&role='+this.role+'&userName='+this.userName+'&pagenum='+this.pagenum +'&pageSize='+this.pageSize
             ).then(res=>res.data).then(res=>{
            
                 if (res.resCode=='000000'){
                     this.tableData=res.data.records
                     this.total=res.data.total

                 }else{
                     alert('获取数据失败')
                 }

             })
         },
        //  获取机构的id和名字
        loadUnit(){
           
            this.$axios.get(this.$httpUrl+'/jk/admin/user/listunit').then(res=>res.data).then(res=>{
             
                 if (res.resCode=='000000'){
                     this.units=res.data
                 }else{
                     alert('获取数据失败')
                 }
                 this.plce();
             })
        }
        },
        beforeMount() {
            this.loadUnit();
           
            this.loadPost();

        }
    }
</script>

<style>

</style>